package mi.payment.utils.transaction;

import android.content.Context;

import java.util.Map;

import mi.payment.bean.db.TradeData;
import mi.payment.constants.CARDTYPE;
import mi.payment.constants.TransactionBitmap;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.secure.TerminalSecure;

/**
 * 9.4.7　基于P B O C 借/贷记IC卡批上送通知交易
 *
 * @author leeshenzhou on 2017/2/8.
 */
class BatchUploadNotificationTask extends TransactionTask<Object, Void, TradeData> {

    BatchUploadNotificationTask(Context context) {
        super(context, false);
    }

    @Override
    protected void onBegin(Object[] objects) {
        tradeData = (TradeData) params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        // 预组装
        return EncodePackage.preAssemblyBase(TransactionBitmap.BITMAP_BATCH_UPLOAD_IC_NOTIFICATION);
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        String netCode = (String) params[1];
        String value62 = (String) params[2];

        // 本地交易时间戳
        dbBean.setTerminalDate(System.currentTimeMillis() / 1000);
        // 交易应答码(域39)
        dbBean.setTradeAnswerCode("96");

        // 更新预组装的Map
        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        // 填写60域
        MsgField fieldF060 = map.get("F060");
        String value60 = "00" + dbBean.getBathNo() + netCode + "60";
        fieldF060.setDataStr(value60);
        fieldF060.setDataLength(value60.length());

        // 填写62域
        MsgField fieldF062 = map.get("F062");
        fieldF062.setDataStr(value62);
        fieldF062.setDataLength(value62.length());

        // 组包，得到组包后的报文
        return EncodePackage.assembly(map, "0320", TerminalSecure.PROCESSREQUEST_NONE);
    }


}
